package com.sskj.fca.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gamerole.orcameralib.CameraActivity;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.fca.R;
import com.sskj.fca.http.HttpConfig;
import com.sskj.fca.presenter.ApplyAgentPresenter;
import com.sskj.fca.util.BitmapUtil;

import java.io.File;
import java.io.IOException;

import butterknife.BindView;

/**
 * Created by Q-Kang on 2019/1/21.
 */

@Route(path = RConfig.APP_APPLY_AGENT)//全民代理申请
public class ApplyAgentActivity extends BaseActivity<ApplyAgentPresenter> {
    @BindView(R.id.et_real_name)
    EditText etRealName;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.iv_idcard_found)
    ImageView ivIdcardFound;
    @BindView(R.id.iv_idcard_back)
    ImageView ivIdcardBack;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    private int card;
    private static final int REQUEST_A_CAMERA = 1000;
    private static final int REQUEST_B_CAMERA = 1001;
    private static final String NAME_A = "photo_a.jpg";
    private static final String NAME_B = "photo_b.jpg";
    private String idCardFrontImg;
    private String idCardBackImg;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_apply_agent;
    }

    @Override
    public ApplyAgentPresenter getPresenter() {
        return new ApplyAgentPresenter();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("全民代理申请");

        //身份证正面照
        ClickUtil.click(ivIdcardFound, () -> {
            Intent intent = new Intent(ApplyAgentActivity.this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    getSaveFile(getApplication(), NAME_A).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
            startActivityForResult(intent, REQUEST_A_CAMERA);
        });
        //身份证背面照
        ClickUtil.click(ivIdcardBack, () -> {
            Intent intent = new Intent(ApplyAgentActivity.this, CameraActivity.class);
            intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,
                    getSaveFile(getApplication(), NAME_B).getAbsolutePath());
            intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
            startActivityForResult(intent, REQUEST_B_CAMERA);
        });
        //申请代理
        ClickUtil.click(btnCommit, () -> {
            if (TextUtils.isEmpty(etRealName.getText().toString().trim())) {
                ToastUtil.showShort("请输入您的这是姓名");
                return;
            }
            if (!CheckUtil.isMobile(etMobile.getText().toString().trim())) {
                ToastUtil.showShort("请输入正确的手机号");
                return;
            }
            if (TextUtils.isEmpty(idCardFrontImg)) {
                ToastUtil.showShort("请上传您的身份证正面照");
                return;
            }
            if (TextUtils.isEmpty(idCardBackImg)) {
                ToastUtil.showShort("请上传您的身份证背面照");
                return;
            }
            mPresenter.applyAgent(etRealName.getText().toString()
                    , etMobile.getText().toString()
                    , idCardFrontImg
                    , idCardBackImg);
        });

    }

    private File getSaveFile(Context context, String failName) {
        return new File(context.getFilesDir(), failName);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case REQUEST_A_CAMERA://身份证正面
                    card = 0;
                    Uri uri = Uri.fromFile(new File(getFilesDir(), NAME_A));
                    try {
                        mPresenter.uploadImg(BitmapUtil.compressImage(MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri)));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case REQUEST_B_CAMERA://身份证背面
                    card = 1;
                    mPresenter.uploadImg(new File(getFilesDir(), NAME_B));
                    break;
            }
        }
    }

    public void updatePath(String path) {
        switch (card) {
            case 0:
                idCardFrontImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, ivIdcardFound);
                break;
            case 1:
                idCardBackImg = path;
                ImageUtil.setImage(HttpConfig.BASE_URL + path, ivIdcardBack);
                break;
        }
    }
}
